package com.e2oto.seller.modules.seller.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseMvpActivity;
import com.e2oto.seller.common.utils.LogTools;
import com.e2oto.seller.common.utils.RxBus;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.seller.adapter.SpecListAdapter;
import com.e2oto.seller.modules.seller.bean.GoodsInfoBean;
import com.e2oto.seller.modules.seller.bean.RxBusBean;
import com.e2oto.seller.modules.seller.bean.SpecBean;
import com.e2oto.seller.modules.seller.presenter.GoodsSpecListPresenter;
import com.e2oto.seller.modules.seller.views.GoodsSpecListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZhangLiQiang on 2017/11/26.
 */

/**
 * 只处理了批发商品 批发goodsId = bean.getBatch_goods_id() 零售goodsId = bean.getGoods_Id()
 */
public class GoodsSpecListActivity extends BaseMvpActivity<GoodsSpecListView, GoodsSpecListPresenter> implements GoodsSpecListView {
    @BindView(R.id.spec_list_recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.goods_spec_list_new_spec_btn)
    Button addSpecBtn;
    @BindView(R.id.add_spec_total_stock)
    EditText totalStockEt;

    private SpecListAdapter adapter;
    private int goodsId;
    private GoodsInfoBean bean;

    @Override
    protected GoodsSpecListPresenter createPresenter() {
        return new GoodsSpecListPresenter();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_goods_spec_list;
    }

    @Override
    protected void afterView(Bundle savedInstanceState) {
        super.afterView(savedInstanceState);
        goodsId = getIntent().getIntExtra("goods_id", -1);

        adapter = new SpecListAdapter(this, new ArrayList<SpecBean>(), new SpecListAdapter.OnClickCommitListener() {
            @Override
            public void onClick() {
                addSpecBtn.setVisibility(View.VISIBLE);
            }
        });
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(true);
        recyclerView.getItemAnimator().setChangeDuration(300);
        recyclerView.getItemAnimator().setMoveDuration(300);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);


        bean = (GoodsInfoBean) getIntent().getSerializableExtra("data");
        if (bean != null) {//代表修改
            goodsId = bean.getBatch_goods_id();
            //初始化原有的数据
            addSpecBtn.setVisibility(View.VISIBLE);
            totalStockEt.setText(bean.getStore_nums() + "");
            for (int i = 0; i < bean.getBatch_spec_array().size(); i++) {
                GoodsInfoBean.BatchSpecArrayBean spec = bean.getBatch_spec_array().get(i);
                SpecBean specBean = new SpecBean();
                specBean.setStore_nums(spec.getStore_nums() + "");
                specBean.setViewType(1);
                specBean.setShowContent(false);
                specBean.setType(spec.getType());
                specBean.setSell_price(spec.getSell_price());
                specBean.setSpec_id(spec.getSpec_id());
                specBean.setUnit(spec.getUnit());
                specBean.setWeight(spec.getWeight());
                adapter.getDataList().add(specBean);
            }
            adapter.notifyDataSetChanged();
        } else {//代表添加
            addSpecBtn.setVisibility(View.GONE);
            adapter.showAddLayout();
        }
    }

    @OnClick({R.id.toolbar_left_btn, R.id.goods_spec_list_new_spec_btn, R.id.goods_spec_list_next_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left_btn:
                finish();
                break;
            case R.id.goods_spec_list_new_spec_btn://添加规格
                addSpecBtn.setVisibility(View.GONE);
                adapter.showAddLayout();
                break;
            case R.id.goods_spec_list_next_btn:

                LogTools.e(adapter.getDataList().toArray().toString());
                String storeTotal = totalStockEt.getText().toString().trim();
                if (TextUtils.isEmpty(storeTotal)) {
                    showMessage("请输入总库存");
                    return;
                }
                mPresenter.addSpec(SellerApp.sellerId, goodsId + "", storeTotal, adapter.getDataList());
                break;

        }
    }

    @Override
    public void addSpecCallback() {
        if (bean != null) {
            RxBus.getIntanceBus().post(new RxBusBean(6654));//通知预览页面刷新数据
        } else {
            Intent intent = new Intent(this, AddGoodsDetailActivity.class);
            intent.putExtra("goods_id", goodsId + "");
            startActivity(intent);
        }
        finish();

    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }
}
